﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace DataAccess
{
	public class CommonHelper
	{
		// The Initialization Vector for the DES encryption routine
		private static readonly byte[] IV = new byte[8] { 232, 43, 12, 65, 0, 4, 5, 2 };

		/// <summary>
		/// Encrypts provided string parameter
		/// </summary>
		public static string Encrypt(string s)
		{
			if(string.IsNullOrEmpty(s))
			{
				return string.Empty;
			}

			string result = string.Empty;

			try
			{
				byte[] buffer = Encoding.ASCII.GetBytes(s);

				TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

				MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();

				des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes("ffffffff"));
				des.IV = IV;

				result = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
			}
			catch
			{
				throw;
			}

			return result;
		}

		/// <summary>
		/// Decrypts provided string parameter
		/// </summary>
		public static string Decrypt(string s)
		{
			if(string.IsNullOrEmpty(s))
			{
				return string.Empty;
			}

			string result = string.Empty;

			try
			{
				byte[] buffer = Convert.FromBase64String(s);

				TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

				MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();

				des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes("cryptoKey"));

				des.IV = IV;

				result = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
			}
			catch
			{
				throw;
			}

			return result;
		}
        //清除html代码
        public static string ClearHtmlCode(string text)
        {
            if (text != null)
            {
                text = text.Trim();
            }
            if (string.IsNullOrEmpty(text)) return string.Empty;
            //排除<数字>的标签，eg："<123>"
            text = Regex.Replace(text, @"<[\/?a-zA-Z]{1,}.*?>", "");
            return text;
        }
        #region 截取字符串
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="stringToSub">原字符串</param>
        /// <param name="length">截取长度</param>
        /// <param name="endstring">截取之后尾部跟随的字符窜如：“...”</param>
        /// <returns></returns>
        public static string GetSubString(string stringToSub, int length, string endstring)
        {
            string str = "";
            if (stringToSub == null)
            {
                return str;
            }
            else
            {
                if (stringToSub.Length > length)
                {
                    str = stringToSub.Substring(0, length) + endstring;
                }
                else
                {
                    str = stringToSub;
                }
                return str;
            }
        }
        #endregion
	}
}
